<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>留言管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <div class="text item">
        <!-- 搜索框区域 -->
        <el-row :gutter="20">
          <el-col :span="7">
            <el-input
              placeholder="请输入内容"
              clearable
              v-model="queryInfo.query"
              @clear="getTopicList"
            >
              <el-button
                slot="append"
                @click="getTopicList"
                icon="el-icon-search"
              ></el-button>
            </el-input>
          </el-col>
        </el-row>
        <!-- 用户列表区 -->
        <el-table :data="TopicList" border stripe>
          <el-table-column
            :show-overflow-tooltip="true"
            width="50"
            type="index"
          ></el-table-column>
          <el-table-column
            :show-overflow-tooltip="true"
            width="200"
            label="标题"
            prop="title"
          ></el-table-column>
          <el-table-column
            :show-overflow-tooltip="true"
            max-width
            label="内容"
            prop="content"
          ></el-table-column>
          <el-table-column
            :show-overflow-tooltip="true"
            width="200"
            label="作者"
            prop="user.username"
          ></el-table-column>
          <el-table-column
            :show-overflow-tooltip="true"
            width="200"
            label="观看量"
            prop="views"
          ></el-table-column>
          <el-table-column
            :show-overflow-tooltip="true"
            width="200"
            label="状态"
          >
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.state"
                :inactive-value="0"
                :active-value="1"
                @change="userStateChanged(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180px">
            <template v-slot="scope">
              <!-- 修改 -->
              <el-button
                style="margin-left: 10px"
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="toEditArticle(scope.row)"
              ></el-button>
              <!-- 删除 -->
              <el-popconfirm
                title="确认删除文章(留言)？"
                style="margin-left: 30px"
                @confirm="deleteUser(scope.row)"
              >
                <el-button
                  slot="reference"
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="queryInfo.pageSize"
          layout="total,sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { pagingTopic, modifyTopic, delTopic } from '../../api/topic';
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 5,
      },
      TopicList: [],
      total: 0,
    };
  },
  methods: {
    async getTopicList() {
      const res = await pagingTopic({
        title: this.queryInfo.query,
        offset: this.queryInfo.pageNum,
        limit: this.queryInfo.pageSize,
      });
      this.TopicList = res.data.rows;
      this.total = res.data.count;
    },
    async userStateChanged(row) {
      const res = await modifyTopic(row);
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: '修改文章状态成功',
        });
      }
    },
    handleSizeChange(size) {
      this.queryInfo.pageSize = size;
      this.getTopicList();
    },
    handleCurrentChange(page) {
      this.queryInfo.pageNum = page;
      this.getTopicList();
    },
    async deleteUser(row) {
      const res = await delTopic(row.topic_id);
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: '删除文章成功',
        });
        this.getTopicList();
      }
    },
    toEditArticle(row) {
      this.$router.push({
        name: 'create',
        params: {
          update: true,
          topic_id: row.topic_id,
        },
      });
    },
  },
  created() {
    this.getTopicList();
  },
};
</script>
